# rename variables
ESS10$maj <- as.numeric(ESS10$imsmetn)

ESS10$min <- as.numeric(ESS10$imdfetn)

ESS10$poor <- as.numeric(ESS10$impcntr)


### RDD ###
rd_maj <- RDestimate(maj ~ date | cntry, data = ESS10, cutpoint = 0, bw = 14, se.type = "HC1")
summary(rd_maj)
rd_min <- RDestimate(min ~ date | cntry, data = ESS10, cutpoint = 0, bw = 14, se.type = "HC1")
summary(rd_min)
rd_poor <- RDestimate(poor ~ date | cntry, data = ESS10, cutpoint = 0, bw = 14, se.type = "HC1")
summary(rd_poor)


### Extract ITT and CI ###
lowmaj <- rd_maj$ci[1,1]
upmaj <- rd_maj$ci[1,2]
coefmaj <- rd_maj$est[1]

lowmin <- rd_min$ci[1,1]
upmin <- rd_min$ci[1,2]
coefmin <- rd_min$est[1]

lowpoor <- rd_poor$ci[1,1]
uppoor <- rd_poor$ci[1,2]
coefpoor <- rd_poor$est[1]


### Plot ITT and CI ###
Outcome2 <- c("Majority", "Minority", "Non-European")
Outcome2 <- as.data.frame(Outcome2)

Coefficient2 <- c(coefmaj, coefmin,coefpoor)
Coefficient2 <- as.data.frame(Coefficient2)

LowerCI2 <- c(lowmaj, lowmin, lowpoor)
LowerCI2 <- as.data.frame(LowerCI2)

UpperCI2 <- c(upmaj, upmin, uppoor)
UpperCI2 <- as.data.frame(UpperCI2)

specgraph2 <- bind_cols(Outcome2, Coefficient2, LowerCI2, UpperCI2)


ggplot(specgraph2, aes(x = Outcome2, y = Coefficient2)) +
  geom_hline(yintercept = 0, color = "black", linetype = "dashed", size = 1) +
  geom_point(size = 8, position = position_dodge(1)) +
  geom_errorbar(aes(ymin = LowerCI2, ymax = UpperCI2), 
                size = 2, width = 0.4, position = position_dodge(1)) +
  theme_light(base_size = 35) +
  xlab("Outcome") +
  ylab("ITT")


